package IO.File;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class File2Demo {
   // todo 不同的操作系统的文件路径分割符不一样，比如mac或者linux可以写 ~ 代表home目录
    private static final String ROOT = "." + File.separator;
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("文件路径分割符:" + File.separator);
        System.out.println("环境变量分割符" + File.pathSeparator);
        //使用File类，依次创建多层文件夹，修改文件夹名字，在指定文件夹创建文件，删除文件夹、删除文件
      /*  File f = new File("a","sss");
        System.out.println(f);*/

    }

    public static File createDirs() {
        List<String> pathList = new ArrayList<>();
        while(true){
            System.out.println("请输入文件路径，如果为空则结束");
            String path = scanner.nextLine();
            if(path == null){
                break;
            }
            pathList.add(path);
        }
        return createDir(pathList.toArray(new String[0]));
    }


    private static File createDir(String... restPaths){
        String rest = joinRestDir(restPaths);
        File dir = new File(ROOT,rest);
        if(dir.exists() && dir.isDirectory()){
            System.out.println("文件夹已经存在" + dir.toString());
        }else{
            boolean createSuccess = dir.mkdirs();
            if(createSuccess){
                return dir;
            }else{
                try {
                    throw new IllegalAccessException("无法在"+ ROOT + "下创建" + rest);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private static String joinRestDir(String... restPaths){
        return Arrays.stream(restPaths).map(String::trim).collect(Collectors.joining(File.separator));
    }
}
